using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowRelevantData = Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;
using WorkflowDataTypes = Scenario.WorkflowEngine.DataModel.WorkflowDataTypes;
using WorkflowParticipantsData = Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData;
using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;

using Scenario.WorkflowEngine;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per FormAttribute.
	/// </summary>
	public class FormAttribute : System.Windows.Forms.Form
	{
		public WorkflowRelevantData.XMLAttribute xmlAttribute;

		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Label Direction;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtName;
		private System.Windows.Forms.ComboBox cmbDirection;
		private System.Windows.Forms.TextBox txtValue;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.ComboBox cmbDataTypes;
		private System.Windows.Forms.ComboBox cmbValues;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormAttribute()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			this.LoadControl();
		}

		public FormAttribute(WorkflowRelevantData.XMLAttribute attribute) : this()
		{
			this.txtName.Text=attribute.Name;
			this.cmbDataTypes.Text=attribute.Type;
			this.cmbDirection.Text=attribute.Direction;
			this.txtValue.Text = attribute.Value;
		}

		private void LoadControl()
		{
			try 
			{
				#region DataTypes
                WorkflowDataTypes.XMLDataTypes m_DataTypes = null;
                m_DataTypes = Engine.DataTypesHelper.GetDataTypes();
				foreach (WorkflowDataTypes.XMLDataType o_DataType in m_DataTypes) 
				{		
					this.cmbDataTypes.Items.Add(o_DataType.Key);
				}
				#endregion

				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_NONE);
				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_INPUTOUTPUT);
				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_CONSTANT);
				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_INPUT);
				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_OUTPUT);
				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_TASK);
				cmbDirection.Items.Add(WorkflowRelevantData.XMLAttribute.DIRECTION_NOTMIGRATE);
			}
			catch (Exception err) 
			{
				Console.WriteLine(err.Message);
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.Direction = new System.Windows.Forms.Label();
            this.cmbDirection = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.cmbDataTypes = new System.Windows.Forms.ComboBox();
            this.lblName = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.txtValue = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.cmbValues = new System.Windows.Forms.ComboBox();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 353);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(492, 42);
            this.panel2.TabIndex = 16;
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(10, 3);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(70, 30);
            this.btnCancel.TabIndex = 6;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(417, 3);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(70, 30);
            this.btnSave.TabIndex = 5;
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // Direction
            // 
            this.Direction.Location = new System.Drawing.Point(7, 108);
            this.Direction.Name = "Direction";
            this.Direction.Size = new System.Drawing.Size(80, 14);
            this.Direction.TabIndex = 22;
            this.Direction.Text = "Direction";
            // 
            // cmbDirection
            // 
            this.cmbDirection.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbDirection.Location = new System.Drawing.Point(7, 122);
            this.cmbDirection.Name = "cmbDirection";
            this.cmbDirection.Size = new System.Drawing.Size(480, 21);
            this.cmbDirection.Sorted = true;
            this.cmbDirection.TabIndex = 2;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(7, 59);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(80, 14);
            this.label3.TabIndex = 20;
            this.label3.Text = "Type";
            // 
            // cmbDataTypes
            // 
            this.cmbDataTypes.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbDataTypes.Location = new System.Drawing.Point(7, 73);
            this.cmbDataTypes.Name = "cmbDataTypes";
            this.cmbDataTypes.Size = new System.Drawing.Size(480, 21);
            this.cmbDataTypes.Sorted = true;
            this.cmbDataTypes.TabIndex = 1;
            this.cmbDataTypes.SelectedIndexChanged += new System.EventHandler(this.cmbDataTypes_SelectedIndexChanged);
            // 
            // lblName
            // 
            this.lblName.Location = new System.Drawing.Point(7, 14);
            this.lblName.Name = "lblName";
            this.lblName.Size = new System.Drawing.Size(86, 14);
            this.lblName.TabIndex = 18;
            this.lblName.Text = "Attribute";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(7, 28);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(480, 20);
            this.txtName.TabIndex = 0;
            // 
            // txtValue
            // 
            this.txtValue.Location = new System.Drawing.Point(7, 180);
            this.txtValue.Multiline = true;
            this.txtValue.Name = "txtValue";
            this.txtValue.Size = new System.Drawing.Size(480, 167);
            this.txtValue.TabIndex = 4;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(7, 166);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(80, 14);
            this.label1.TabIndex = 24;
            this.label1.Text = "Value";
            // 
            // cmbValues
            // 
            this.cmbValues.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbValues.Enabled = false;
            this.cmbValues.Location = new System.Drawing.Point(93, 159);
            this.cmbValues.Name = "cmbValues";
            this.cmbValues.Size = new System.Drawing.Size(394, 21);
            this.cmbValues.Sorted = true;
            this.cmbValues.TabIndex = 3;
            this.cmbValues.SelectedIndexChanged += new System.EventHandler(this.cmbValues_SelectedIndexChanged);
            // 
            // FormAttribute
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(492, 395);
            this.ControlBox = false;
            this.Controls.Add(this.cmbValues);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtValue);
            this.Controls.Add(this.Direction);
            this.Controls.Add(this.cmbDirection);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.cmbDataTypes);
            this.Controls.Add(this.lblName);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.panel2);
            this.Name = "FormAttribute";
            this.Text = "Attribute";
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			xmlAttribute = new WorkflowRelevantData.XMLAttribute();
			xmlAttribute.Name = this.txtName.Text;
			xmlAttribute.Type = this.cmbDataTypes.Text;
			xmlAttribute.Direction = this.cmbDirection.Text;
			xmlAttribute.Value = this.txtValue.Text;

			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void cmbDataTypes_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.cmbValues.Items.Clear();

			#region DataTypes
			WorkflowDataTypes.XMLEnumerationValues vValues = null;
			
			switch (cmbDataTypes.Text )
			{
				case WorkflowDataTypes.XMLDataType.PARTICIPANTS_DATATYPE:
					vValues = new WorkflowDataTypes.XMLEnumerationValues();
                    WorkflowParticipantsData.XMLUsers xmlUsers = Engine.UserHelper.GetUsers();
					
					foreach ( WorkflowParticipantsData.XMLUser user in xmlUsers)
					{
						WorkflowDataTypes.XMLDataTypeValue xmlValue =  new WorkflowDataTypes.XMLDataTypeValue ();
						xmlValue.Key = user.Key;

						vValues.Add (xmlValue); 
					}
					break;
				case WorkflowDataTypes.XMLDataType.WORKFLOWPROCESSES_DATATYPE:
					vValues = new WorkflowDataTypes.XMLEnumerationValues();
                    WorkflowProcesses.XMLWorkflows xmlWorkflows = Scenario.WorkflowEngine.Engine.ProcessHelper.GetWorkflowsTemplate();
					WorkflowProcesses.XMLWorkflow xmlWorkflow = null;
					int iPosIndex = -1;
					for ( int i=0; i<xmlWorkflows.Count;i++)
					{
						xmlWorkflow = (WorkflowProcesses.XMLWorkflow)xmlWorkflows[i];
						iPosIndex = xmlWorkflow.Name.IndexOf (".");

						WorkflowDataTypes.XMLDataTypeValue xmlValue =  new WorkflowDataTypes.XMLDataTypeValue ();
						if ( iPosIndex >=0 ) 
						{
							
							xmlValue.Key = xmlWorkflow.Name.Substring (0,iPosIndex);
						}
						else
						{
							xmlValue.Key = xmlWorkflow.Name;
						}
						vValues.Add (xmlValue); 

					}
					break;
				default:
                    try
                    {
                        vValues = Engine.DataTypesHelper.GetValues(cmbDataTypes.Text);
                    }
                    catch (Exception err)
                    {
                        System.Diagnostics.Debug.WriteLine(err.Message);
                    }
					break;
			}

			if ( vValues != null )
			{
				WorkflowDataTypes.XMLDataTypeValue xmlValue = null;

				for ( int i=0; i<vValues.Count; i++)
				{
					xmlValue = (WorkflowDataTypes.XMLDataTypeValue)vValues[i];

					this.cmbValues.Items.Add ( xmlValue.Key);
				}

				this.cmbValues.Enabled = true;
			}
			else
			{
				this.cmbValues.Enabled = false;
			}

			#endregion
		}

		private void cmbValues_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			#region DataTypes
			WorkflowDataTypes.XMLEnumerationValues vValues = null;
			
			switch (cmbDataTypes.Text )
			{
				case WorkflowDataTypes.XMLDataType.PARTICIPANTS_DATATYPE:
				case WorkflowDataTypes.XMLDataType.WORKFLOWPROCESSES_DATATYPE:
					this.txtValue.Text = this.cmbValues.Text;
					break;

				default:
				{
                    vValues = Engine.DataTypesHelper.GetValues(cmbDataTypes.Text);
					if ( vValues != null )
					{
						WorkflowDataTypes.XMLDataTypeValue xmlValue = null;

						for ( int i=0; i<vValues.Count; i++)
						{
							xmlValue = (WorkflowDataTypes.XMLDataTypeValue)vValues[i];

							if ( xmlValue.Key == cmbValues.Text ) 
							{
								this.txtValue.Text = xmlValue.Value;
							}
						}
					}

					break;
				}
			}
			#endregion
		}
	}
}
